#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;

int Find(vector<string>& strs, string& s, int pos)
{
    for (int i = pos; i < strs.size(); ++i)
    {
        if (strs[i] == s)
        {
            pos = i;
            return i;
        }
    }
    return -1;
}

int main()
{
    int n;
    cin >> n;
    vector<string> strs(n);
    string s1;
    string s2;
    cin >> s1 >> s2;
    string a;
    for (int i = 0; i < n; ++i)
    {
        cin >> a;
        strs[i] = a;
    }

    vector<int> ret;
    int pos1 = Find(strs, s1, 0), pos2 = Find(strs, s2, 0);
    if (pos1 == -1 || pos2 == -1)
    {
        cout << "-1" << endl;
        return 0;
    }
    ret.emplace_back(abs(pos1 - pos2));

    sort(ret.begin(), ret.end());
    cout << ret[ret.size() - 1] << endl;

    return 0;
}

class Solution {
public:
    int triangleNumber(vector<int>& nums) {
        sort(nums.begin(), nums.end());

        int ret = 0, n = nums.size();
        for (int i = n - 1; i >= 2; --i)
        {
            int left = 0, right = i - 1;
            while (left < right)
            {
                if (nums[left] + nums[right] > nums[i])
                {
                    ret += right - left;
                    --right;
                }
                else
                    ++left;
            }
        }
        return ret;
    }
};